package chances.epg.ui.taglib.html;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

import com.google.common.base.Strings;

import chances.epg.ui.taglib.layout.BaseLayoutTag;
import chances.epg.ui.taglib.model.LinkModel;

public class TextTag extends BaseLayoutTag {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String DOT = "…";
	protected int length;
	private boolean warp;
	private boolean marquee;

	private String linkId;
	private String var;
	private String ellipsis = "...";

	protected String text = ""; // 文本
	protected String defaultText = "";

	private String regExpr;
	private String icon;

	protected String prefix;// 需要去除的前缀

	@Override
	public void permutate() {
		super.permutate();
	}

	protected String varEqual;// 处理前后是否一样

	@Override
	public void doAfterStart() throws JspException {

		if (Strings.isNullOrEmpty(this.getVm())) {
			this.setVm("text");
		}
		super.doAfterStart();
	}

	@Override
	public int doAfterBody() throws JspException {
		if (StringUtils.isEmpty(this.getTitle())) {
			this.text = this.bodyContent.getString();
		} else {
			this.text = this.getTitle();
		}
		if (this.length > 0) {
			this.setTitle(subStr(this.text, length, ellipsis));
			this.marquee = true;
			LinkTag alinkTag = super.findLinkTag();

			if (alinkTag == null) {
				if (StringUtils.isNotEmpty(this.getForId())) {
					LinkModel model = epgPageContext.findLink(this.getForId());
					if (model != null) {
						model.addEffect(this.getId(), "marquee");
					}

				} 
			}else {
				alinkTag.addEffect(this.getId(), "marquee");
			}

		} else {
			this.setTitle(this.text);
		}
		if (StringUtils.isNotEmpty(var)) {
			pageContext.setAttribute(this.var, this.getTitle());
		}

		return super.doAfterBody();
	}

	public boolean isWarp() {
		return warp;
	}

	public void setWarp(boolean warp) {
		this.warp = warp;
	}

	public String getLinkId() {
		return linkId;
	}

	public void setLinkId(String linkId) {
		this.linkId = linkId;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public boolean isMarquee() {
		return marquee;
	}

	public void setMarquee(boolean marquee) {
		this.marquee = marquee;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public static String subStr(String text, int max, String ellipsis) {
		char[] textChars = text.toCharArray();
		StringBuffer buffer = new StringBuffer();
		int pos = 0;
		int length = max * 2;
		for (int i = 0; i < textChars.length; i++) {
			char aChar = textChars[i];
			if (CharUtils.isAsciiPrintable(aChar)) {
				pos++;
			} else {
				pos = pos + 2;
			}
			if (pos <= length) {
				buffer.append(aChar);
			} else {
				break;
			}
		}
		if (buffer.length() < text.length()) {
			buffer.replace(buffer.length() - 1, buffer.length(), ellipsis);
		}
		return buffer.toString();
	}

	public static String createParagraph(String text, int fontSize, int width, int height, String lineBreak,
			String ellipsis) {
		char[] textChars = text.toCharArray();
		int maxWord = width / fontSize;
		int maxLine = height / fontSize;
		StringBuffer buffer = new StringBuffer();
		int pos = 0;
		int col = 0;
		int count = 0;
		int length = maxWord * 2;

		for (int i = 0; i < textChars.length; i++) {
			char aChar = textChars[i];
			if (CharUtils.isAsciiPrintable(aChar)) {
				pos++;
			} else {
				pos = pos + 2;
			}
			if (pos <= length) {
				buffer.append(aChar);
				count++;
			}
			if (pos >= length) {
				if (col < maxLine - 1) {
					buffer.append(lineBreak);
				} else {
					break;
				}
				pos = 0;
				col++;
			}

		}
		if (StringUtils.isNotEmpty(ellipsis)) {
			if (count < textChars.length) {
				buffer.replace(buffer.length() - 1, buffer.length(), ellipsis);
			}
		}
		return buffer.toString();
	}

	public String getEllipsis() {
		return ellipsis;
	}

	public void setEllipsis(String ellipsis) {
		this.ellipsis = ellipsis;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getRegExpr() {
		return regExpr;
	}

	public void setRegExpr(String regExpr) {
		this.regExpr = regExpr;
	}

	public String getDefaultText() {
		return defaultText;
	}

	public void setDefaultText(String defaultText) {
		this.defaultText = defaultText;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getVarEqual() {
		return varEqual;
	}

	public void setVarEqual(String varEqual) {
		this.varEqual = varEqual;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

}
